package com.hzw.code.common.constant;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzw.code.common.utils.CustomStringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 基础入参参数(代分页)
 *
 * @author 胡汉三
 * @date 2020-05-18 15:12
 */
@Getter
@Setter
@ToString
@ApiModel(value = "分页请求数据request", description = "分页请求参数")
public class SuperPageArgs implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页
     */
    @ApiModelProperty(name = "currentPage", value = "当前页" ,example = "1")
    private Long currentPage;

    /**
     * 每页条数
     */
    @ApiModelProperty(name = "size", value = "每页条数",example = "10")
    private Long size;

    /**
     * 降序字段列表
     */
    @ApiModelProperty(name = "descs", value = "降序字段列表")
    private List<String> descs;

    @ApiModelProperty(name = "ascs", value = "升序字段列表")
    private List<String> ascs;

    /**
     * 开始时间
     */
    @ApiModelProperty(name = "startTime", value = "开始时间", example = "2019-10-26 11:24")
    private Date startTime;

    /**
     * 结束时间
     */
    @ApiModelProperty(name = "endTime", value = "结束时间", example = "2019-10-26 11:24")
    private Date endTime;

    /**
     * myBatisPlus 分页信息
     * @param <T>
     * @return
     */
    @ApiModelProperty(name = "queryPage", value = "myBatisPlus 分页信息", hidden = true)
    public <T> Page<T> getQueryPage() {
        if (this.currentPage == null || this.currentPage < 0) {
            this.currentPage = 0L;
        }
        if (this.size == null || this.size <= 0) {
            this.size = 10L;
        }
        Page<T> page = new Page<>(this.currentPage, this.size);
        if (this.descs != null && !this.descs.isEmpty()) {
            this.descs.forEach(v -> {
                OrderItem orderItem = OrderItem.desc(CustomStringUtils.humpToUnderline(v));
                page.addOrder(orderItem);
            });
        } else if (this.ascs != null && !this.ascs.isEmpty()) {
            this.ascs.forEach(v -> {
                OrderItem orderItem = OrderItem.asc(CustomStringUtils.humpToUnderline(v));
                page.addOrder(orderItem);
            });
        }
        return page;
    }
}

